package com.example.springbootdemo.listener;

import com.example.springbootdemo.scheduler.QuartzJobScheduler;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;

/**
 * 监听spring容器加载完毕后事件，启动任务调用
 * @author hh
 * @date 2022/1/25
 */
@Slf4j
@Configuration
public class ApplicationStartQuartzJobListener implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private QuartzJobScheduler quartzJobScheduler;

    /**
     * 初始启动quartz
     * @param event event
     */
    @Override
    public void onApplicationEvent(@NotNull ContextRefreshedEvent event) {
        try {
            quartzJobScheduler.startAllJob();
            log.info("Quartz初始化启动成功...");
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

}
